package de.lmu.ifi.dbs.trafficmining.ui;

import de.lmu.ifi.dbs.trafficmining.Statistics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

public class StatisticsFrame extends javax.swing.JFrame {

    /**
     * Creates new form StatisticsFrame
     */
    public StatisticsFrame() {
        initComponents();
        generalModel.setColumnCount(0);
        generalModel.addColumn("Key");
        generalModel.addColumn("Value");
        // -
        pathModel.setColumnCount(0);
    }

    public void clear() {
        generalModel.setRowCount(0);
        pathModel.setRowCount(0);
        pathModel.setColumnCount(0);
    }

    public void setData(Statistics data) {
        // re initalize the model
        generalModel.setRowCount(0);

        ArrayList<String> keys = new ArrayList<>(data.getAll().keySet());
        Collections.sort(keys);

        int i = 0;
        for (String key : keys) {
            generalModel.setNumRows(i + 1);
            generalModel.setValueAt(key, i, 0);
            generalModel.setValueAt(data.get(key), i, 1);
            i++;
        }
    }

    public void addPathData(Map<String, String> data, int resultId) {
        // re initalize the model
        ArrayList<String> keys = new ArrayList<>(data.keySet());
        Collections.sort(keys);

        // build first column
        if (pathModel.getColumnCount() == 0) {
            pathModel.addColumn("Path:");
            pathModel.setNumRows(data.size());
            for (int i = 0; i < keys.size(); i++) {
                String key = keys.get(i);
                pathModel.setValueAt(key, i, 0);

            }
        }

        // add data
        int col = pathModel.getColumnCount();
        pathModel.addColumn(resultId + "");
        for (int i = 0; i < keys.size(); i++) {
            pathModel.setValueAt(data.get(keys.get(i)), i, col);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        generalModel = new javax.swing.table.DefaultTableModel();
        pathModel = new javax.swing.table.DefaultTableModel();
        javax.swing.JSplitPane splitpane = new javax.swing.JSplitPane();
        javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
        javax.swing.JLabel generalHead = new javax.swing.JLabel();
        javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane();
        generalStatsTable = new javax.swing.JTable();
        javax.swing.JPanel jPanel2 = new javax.swing.JPanel();
        pathHead = new javax.swing.JLabel();
        javax.swing.JScrollPane scrollPane2 = new javax.swing.JScrollPane();
        pathStatsTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Statistics");
        setAlwaysOnTop(true);

        splitpane.setDividerLocation(150);
        splitpane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        splitpane.setPreferredSize(new java.awt.Dimension(300, 300));

        jPanel1.setLayout(new java.awt.BorderLayout());

        generalHead.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        generalHead.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        generalHead.setText("General Statistics");
        jPanel1.add(generalHead, java.awt.BorderLayout.PAGE_START);

        generalStatsTable.setModel(generalModel);
        scrollPane.setViewportView(generalStatsTable);

        jPanel1.add(scrollPane, java.awt.BorderLayout.CENTER);

        splitpane.setTopComponent(jPanel1);

        jPanel2.setLayout(new java.awt.BorderLayout());

        pathHead.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        pathHead.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pathHead.setText("Path Statistics");
        jPanel2.add(pathHead, java.awt.BorderLayout.PAGE_START);

        pathStatsTable.setModel(pathModel);
        scrollPane2.setViewportView(pathStatsTable);

        jPanel2.add(scrollPane2, java.awt.BorderLayout.CENTER);

        splitpane.setBottomComponent(jPanel2);

        getContentPane().add(splitpane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.table.DefaultTableModel generalModel;
    private javax.swing.JTable generalStatsTable;
    private javax.swing.JLabel pathHead;
    private javax.swing.table.DefaultTableModel pathModel;
    private javax.swing.JTable pathStatsTable;
    // End of variables declaration//GEN-END:variables
}
